import MD5 from "./md5";
import {
    		emojiName,
    		emojiUrl,
    		emojiMap
    	} from "@/components/base/emojiMap";
/**
 * 生成随机数
 * @param {Number} len 长度
 */
export function genSignNonce(len = 16) {
	//随机字符串
	const keyChars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
		"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
		"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
	];
	let nonce = "";
	for (let i = 0; i < len; i += 1) {
		nonce += keyChars[Math.floor(Math.random() * 62)];
	}
	return nonce;
}

/**
 * 获取当前时间戳
 * @returns {BigInteger}
 */
export const getTimestamp = () => {
	return new Date().getTime() / 1000;
};

/**
 * 获取当前页面随机数
 * @reutrns {String}
 */
export const getPageRandom = (arg = []) => {
	return MD5(arg.join("&"));
};

/**
 * 获取用户系统信息
 * @returns {Object}
 */
export const getSystemInfo = () => {
	let systemInfo = uni.getStorageSync('systemInfo') || false;
	// console.log("systemInfo", systemInfo)
	if (systemInfo == false) {
		uni.getSystemInfo({
			success(res) {
				systemInfo = res;
				uni.setStorageSync('systemInfo', systemInfo);
			}
		});
	}
	return systemInfo;
};

/**
 * 获取UserIdentity
 * @returns {string}
 */
export function getUserIdentityKey() {
	let identity = uni.getStorageSync('userIdentityKey') || false;
	if (identity == false) {
		identity = MD5(genSignNonce + getTimestamp()).toLocaleLowerCase();
		uni.setStorageSync('userIdentityKey', identity);
	}
	return identity;
}

export function $goPage(path, params, bottom) {
		uni.$u.route({
			url: `/pages${path}/index`,
			params,
			animationType: bottom ? 'slide-in-bottom' : 'pop-in'
		})
}
//解析含有表情包的字符串(发布社区、动态等等时)
export function parseText(content) {
				let renderDom = [];
				let temp = content;
				let left = -1;
				let right = -1;

				while (temp !== '') {
					left = temp.indexOf('[');
					right = temp.indexOf(']');

					switch (left) {
						case 0:
							if (right === -1) {
								renderDom.push({
									name: 'span',
									text: temp
								});
								temp = '';
							} else {
								const _emoji = temp.slice(0, right + 1);

								if (emojiMap[_emoji]) {
									renderDom.push({
										name: 'img',
										src: emojiUrl + emojiMap[_emoji]
									});
									temp = temp.substring(right + 1);
								} else {
									renderDom.push({
										name: 'span',
										text: '['
									});
									temp = temp.slice(1);
								}
							}

							break;

						case -1:
							renderDom.push({
								name: 'span',
								text: temp
							});
							temp = '';
							break;

						default:
							renderDom.push({
								name: 'span',
								text: temp.slice(0, left)
							});
							temp = temp.substring(left);
							break;
					}
				}

				return renderDom;
			}
